package conversions;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import model.AdminMessage;
import model.Folder;
import model.GeneralMessage;
import model.History;
import model.Membership;
import model.Repository;
import model.Resource;
import model.ServerLogEntry;
import model.User;
import dao.FolderDao;
import dao.ResourceDao;
import dao.UserDao;
import dto.AdminMessageDTO;
import dto.DTOUser;
import dto.FolderDTO;
import dto.GeneralMessageDTO;
import dto.HistoryDTO;
import dto.RepositoryDTO;
import dto.ResourceDTO;
import dto.ServerLogEntryDTO;
import enumerations.MessageType;
import enumerations.Type;
import enumerations.UserType;




@Stateless
@LocalBean
public class Conversions {
	
	@EJB
	UserDao userDao;
	
	@EJB
	FolderDao folderDao;
	
	@EJB
	ResourceDao resDao;
	
    public Conversions() {
    }
    
    /**
     * Description: Getting a new Repository having name value of repName(from form)
     * @author Zsolt
     * @param dto RepositoryDTO
     * @param rootF 
     * @return Repository 
     */
    public Repository fromDTOtoRepository(RepositoryDTO dto, Folder rootF)
    {
    	Repository r = new Repository();
    	r.setName((String)dto.getRepName());
    	r.setVersion(1);
    	r.setRepoUrl("RepositoryFolder/" + (String)dto.getRepName());
    	r.setFolder(rootF);
    	return r;
    }
    
    /**
     * Description: Getting a new Membership having Folder and User value from dao
     * @author Zsolt
     * @param dto RepositoryDTO
     * @param user 
     * @param folder 
     * @return Memebership
     */
    public Membership fromDTOtoMembership(RepositoryDTO dto, Folder folder, User user) {
		Membership m = new Membership();
		m.setFolder(folder);
		m.setUser(user);
		m.setSupervisor(1);
		return m;
	}
    
    /**
     * Description: Getting a new user having user.name value of username(from form)
     * 
     * @param u DTOUser 
     * @return User 
     */
    public User fromDTOUser(DTOUser u){
    	User user;
    	user = new User();
    	user.setName(u.getName());
    	return user;
    }

    /**
     * Description: Getting a new history object from HistoryDTO
     *  
     * @param hDto  {@link HistoryDTO}
     * @return {@link History}
     */
    public History fromDTOtoHistory(HistoryDTO hDto){
    	History h;
    	h= new History();
    	h.setId(hDto.getId());
    	h.setDescription(hDto.getDescription());
    	h.setDate(hDto.getData());
    	Type typeReceived=null;
		for (Type logtype : Type.values()) {
			if(logtype.toString().equals(hDto.getType())){
				typeReceived= logtype;
			}
		}
    	h.setType(typeReceived);
		return h; 	
    }
    
    /**
     * Ienei Robert
     * conversie din DTOUser in entitate User
     */
  
    public User userFromDto(DTOUser uDto) {
    	
		User u = new User();
		u.setEmail((String)uDto.getEmail());
		u.setName((String)uDto.getName());
		
		u.setPassword((String)uDto.getPassword());
		u.setTelephone((String)uDto.getTelephone());
		if (uDto.getType().equals("user"))
			u.setUserType(UserType.user);
		else
			if (uDto.getType().equals("admin"))
				u.setUserType(UserType.admin);
			else 
				if (uDto.getType().equals("invalidated"))
				u.setUserType(UserType.invalidated);
		return u;
	}


    /**
     * Ienei Robert
     * convert User to DTOUser
     * 
     */
	public DTOUser fromUserToDto(User user) {
		DTOUser uDto = new DTOUser();
		uDto.setId(user.getId());
		uDto.setName(user.getName());
		uDto.setPassword(user.getPassword());
		uDto.setEmail(user.getEmail());
		uDto.setTelephone(user.getTelephone());
		uDto.setType(user.getUserType().toString());
		return uDto;
	}

    /**
     * Ienei Robert
     * convert Repository to RepositoryDto
     * 
     */
	public RepositoryDTO fromRepositoryToDto(Repository rep) {
		RepositoryDTO rDto = new RepositoryDTO();
		rDto.setId(rep.getId());
		rDto.setRepName(rep.getName());
		return rDto;
	}

    /**
     * @author soreas
     * @param pResource Obiect de tip Resource, care va fi transformat in ResourceDTO
     * @return
     */
    public ResourceDTO resourceToDTO(Resource pResource){
    	ResourceDTO rDTO=new ResourceDTO(pResource.getId(), pResource.getName(), pResource.getFolder().getId(), pResource.getVersion(), pResource.getContent(), pResource.getLockUser().getId(), pResource.getPath());
    	return rDTO;
    }
    
    /**
     * transforma un model de AdminMessage intr-un GeneralMessageDTO
     * @author zirbom
     * @param adminMessage
     * @return newMailDTO
     */
    public GeneralMessageDTO adminMessageModelTODto (AdminMessage adminMessage){
    	
    	GeneralMessageDTO newMailDTO = new GeneralMessageDTO();
    	
    	
    	newMailDTO.setId(adminMessage.getId());
    	newMailDTO.setContent(adminMessage.getContent());
    	newMailDTO.setDate(adminMessage.getDate());
    	newMailDTO.setIdSender(adminMessage.getSender().getId());
    	newMailDTO.setType(adminMessage.getMessageType().toString());
    	newMailDTO.setSender(userDao.getUserById(adminMessage.getSender().getId()).getName());
    	
		return newMailDTO;
    	
    }
    /**
     * @author soreas
     * 
     * @param pFolder Obiect de tip Folder, ce va fi transformat in DTO
     * @return obiect de tip FolderDTO
     */
   
    public FolderDTO fromFolderToDTO(Folder pFolder){
   	 	FolderDTO folderDTO=new FolderDTO();
   	 	folderDTO.setId(pFolder.getId());
   	 	folderDTO.setId_parent(pFolder.getFolder().getId());
   	 	folderDTO.setName(pFolder.getName());
   	 	folderDTO.setVersion(pFolder.getVersion());
   	 	folderDTO.setIdRepository(pFolder.getRepository().getId());
   	 	return folderDTO;
    }	
   
    
    /**
     * @author tokea
     * @param resource
     * @return ResourceDTO
     */
    public ResourceDTO fromResourceToDTO(Resource resource){
    	ResourceDTO dto = new ResourceDTO();
    	dto.setContent(resource.getContent());
    	dto.setFolderId(resource.getFolder().getId());
    	dto.setId(resource.getId());
    	dto.setIdRepository(resource.getRepository().getId());
    	dto.setLockerId(resource.getLockUser().getId());
    	dto.setName(resource.getName());
    	dto.setVersion(resource.getVersion());
    	return dto;
    }

    /**
     * @author tokea
     * @param flist
     * @return ArrayList<FolderDTO>
     */
    public ArrayList<FolderDTO> fromFolderListToDTO(List<Folder> flist){
    	ArrayList<FolderDTO> dtolist = new ArrayList<FolderDTO>();
    	for(Folder f:flist)
    		dtolist.add(fromFolderToDTO(f));
    	return dtolist;
    }
    
    
    /**
     * @author tokea
     * @param rlist
     * @return ArrayList<ResourceDTO>
     */
	public ArrayList<ResourceDTO> fromResourceListToDTO(List<Resource> rlist){
		ArrayList<ResourceDTO> dtolist = new ArrayList<ResourceDTO>();
		for(Resource r:rlist)
    		dtolist.add(fromResourceToDTO(r));
    	return dtolist;
	}
	
	/**
	 * @author fodorl
	 * @param messType
	 * @return MessageType
	 * converteste un string care este tipul mesajului intr-un MessageType
	 */
	
	public MessageType getMessageTypeFromString(String messType){
		
		for(MessageType messageType: MessageType.values()){
			if(messType.equals(messageType.toString())){
				return messageType;
			}
		}
		return null;
	}
	
	/**
	 * @author fodorl
	 * @param adminMessageDTO
	 * @return AdminMessage
	 * converteste un AdminMessageDTO intr-un AdminMessage
	 */
	
	public AdminMessage adminMessageFromDTO(AdminMessageDTO adminMessageDTO){
		
		AdminMessage adminMessage = new AdminMessage();
		
		adminMessage.setDate(adminMessageDTO.getDate());
		adminMessage.setContent(adminMessageDTO.getContent());
		adminMessage.setMessageType(getMessageTypeFromString(adminMessageDTO.getType()));
		adminMessage.setSender(userDao.getUserById(adminMessageDTO.getIdSender()));
		
		return adminMessage;
	}
	
	/**
	 * @author fodorl
	 * @param generalMessageDTO
	 * @return GeneralMessage
	 * conversteste un GeneralMessageDTO intr-un GeneralMessage
	 */
	
	public GeneralMessage generalMessageFromDTO(GeneralMessageDTO generalMessageDTO){
		
		GeneralMessage generalMessage = new GeneralMessage();
		
		generalMessage.setContent(generalMessageDTO.getContent());
		generalMessage.setDate(generalMessageDTO.getDate());
		generalMessage.setSender(userDao.getUserById(generalMessageDTO.getIdSender()));
		generalMessage.setFolder(folderDao.getFolderByID(generalMessageDTO.getIdFolder()));
		generalMessage.setMessageType(getMessageTypeFromString(generalMessageDTO.getType()));
		return generalMessage;
	}
	
	 /**
     * transforma un model de GeneralMessage intr-un GeneralMessageDTO
     * @author zirbom
     * @param generalMessage
     * @return newMailDTO
     */
    public GeneralMessageDTO generalMessageModelTODto (GeneralMessage generalMessage){
    	
    	GeneralMessageDTO newMailDTO = new GeneralMessageDTO();
    	
    	newMailDTO.setId(generalMessage.getId());
    	newMailDTO.setContent(generalMessage.getContent());
    	newMailDTO.setDate(generalMessage.getDate());
    	newMailDTO.setIdSender(generalMessage.getSender().getId());
    	newMailDTO.setType(generalMessage.getMessageType().toString());
    	newMailDTO.setIdFolder(generalMessage.getFolder().getId());
    	newMailDTO.setSender(userDao.getUserById(generalMessage.getSender().getId()).getName());
    	
    	
		return newMailDTO;
    	
    }
	
    /**
     * @author Zsombor
     * @param param 
     * @return ArrayList<ServerLogEntryDTO>
     */
	
	public List<ServerLogEntryDTO> serverLogsToDTOs(List<ServerLogEntry> param){
		
		List<ServerLogEntryDTO> result = new ArrayList<ServerLogEntryDTO>();
		
		for(ServerLogEntry sle:param){
			ServerLogEntryDTO s = new ServerLogEntryDTO();
			s.setId(sle.getId());
			s.setContent(sle.getContent());
			SimpleDateFormat formatter = new SimpleDateFormat();
			formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
			String formattedDate = formatter.format(sle.getDate());

			s.setDate(formattedDate);
			s.setLogType(sle.getLogType().toString());
			s.setSender(userDao.getUserById(sle.getSender().getId()).getName());
			
			result.add(s);
		}
		
		return result;
	}
	
	/**
	 *  @author horea
	 * @param re list of repositories
	 * @return list of rdtos
	 */
    public List<RepositoryDTO> repToDTO(List<Repository> re){
    	
    	List<RepositoryDTO> rd = new ArrayList<RepositoryDTO>();
    	
    	for(Repository r : re ){
    		RepositoryDTO rDTO = new RepositoryDTO();
    		
    		rDTO.setRepID(r.getId().toString());
    		rDTO.setRepName(r.getName());
    		
    		
    		rd.add(rDTO);
    	}
    	
    	return rd;
    }
	
}
